package br.com.iswe.recipemaster.dao.security;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.UserDetails;

import br.com.iswe.core.security.ISWEPrincipal;
import br.com.iswe.recipemaster.entities.security.Role;
import br.com.iswe.recipemaster.entities.security.RoleGroup;
import br.com.iswe.recipemaster.entities.security.User;

public class UserDetailsImpl implements UserDetails, ISWEPrincipal {
	private static final long serialVersionUID = 1L;

	private User user;
	
	//IoC
	private UserDAO userDAO;
	
	public UserDetailsImpl(User user){
		this.user = user;
	}
	
	public List<Role> getRoleList(RoleGroup roleGroup){
		/*load all the depended objects
		if(roleGroup.getRoleList() == null || roleGroup.getRoleList().size() ==0){
			roleGroupDAO.fetchList(roleGroup, RoleGroup.ROLE_LIST, roleGroup.getRoleListKeys());
		}
		*/
		
		List<Role> lista = new ArrayList<Role>();
		lista.addAll(roleGroup.getRoleList());
		for (final RoleGroup subRoleGroup : roleGroup.getRoleGroupList()) {
			lista.addAll(getRoleList(subRoleGroup));
		}
		return lista;
	}
	
	
	private List<Role> lista;
	public List<Role> getRoleList(){
		//load all the depended objects
		if(user.getRoleList() == null || user.getRoleList().size() ==0){
			userDAO.fetchList(user, User.ROLE_LIST, user.getRoleKeyList());
		}
		if(user.getRoleGroupList() == null || user.getRoleGroupList().size() ==0){
			userDAO.fetchList(user, User.ROLE_GROUP_LIST, user.getRoleGroupKeyList());
		}
		
		if(lista == null){
			lista = new ArrayList<Role>();
			lista.addAll(user.getRoleList());
			if(user.getRoleGroupList() != null){
				for (final RoleGroup roleGroup : user.getRoleGroupList()) {
					lista.addAll(getRoleList(roleGroup));
				}
			}
		}
		return lista;
	}

	public String getIdUsuario() {
		return user.getEmail();
	}

	public String getNomeUsuario() {
		return user.getNickName();
	}

	private GrantedAuthority[] authorities;
	public GrantedAuthority[] getAuthorities() {
		if(authorities == null){
			List<GrantedAuthority> lista = new ArrayList<GrantedAuthority>();
			for (final Role role : getRoleList()) {
				lista.add(new GrantedAuthority(){
					private static final long serialVersionUID = 1L;
	
					public String getAuthority() {
						return role.getRoleName();
					}

					@Override
					public int compareTo(Object o) {
						//obrigatorio com a migracao de acegi para spring security
						return 0;
					}
				});
			}
			authorities = lista.toArray(new GrantedAuthority[]{});
		}
		return authorities;
	}
	
	public String getPassword() {
		return null;
	}

	public String getUsername() {
		return user.getEmail();
	}

	public boolean isAccountNonExpired() {
		return user.isValid();
	}

	public boolean isAccountNonLocked() {
		return user.getBlocked() != null ? ! user.getBlocked() : true;
	}

	public boolean isCredentialsNonExpired() {
		return user.isValid();
	}

	public boolean isEnabled() {
		return user.getBlocked() != null ? ! user.getBlocked() : true;
	}

	public User getUser() {
		return user;
	}
	
	public UserDAO getUserDAO() {
		return userDAO;
	}
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
}
